﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
using CodingLei_Model.ExtendModel;
using CodingLei_Model.SearchModel;
using UserInfoModel = CodingLei_Model.TableModel.UserInfoModel;

namespace CodingLei_Admin.Controllers.Account
{

    public class UserInfoController : Controller
    {
        public static string path = System.Web.HttpContext.Current.Server.MapPath("/Models/") + "data5.xml";

        // GET: UserInfo
        public ActionResult UserInfoIndex()
        {
            var a = InfoList().Select(m => m.UserId).Max();
            ViewData["MaxUserId"] = ++a;
            return View();
        }
        /// <summary>
        /// 会员信息加载
        /// </summary>
        /// <param name="search"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetUserInfoList(DemoSearchModel search, int id = 0)
        {
            var resultModel = InfoList();
            return PartialView("UserInfoResultView", resultModel);
        }

        public IList<UserInfoModel> InfoList()
        {
            XElement userInfoXml = XElement.Load(path);
            IList<UserInfoModel> modelList = new List<UserInfoModel>();
            var a = modelList.Where(m => m.UserId == 1).Select(m => m.UserId).ToList();
            try
            {
                modelList =
                    userInfoXml.Descendants("User")
                        .Select(
                            x =>
                                new UserInfoModel
                                {
                                    UserId = Convert.ToInt32(x.Attribute("UserId").Value),
                                    UserName = x.Element("UserName").Value.ToString(),
                                    Tell = x.Element("Tell").Value.ToString(),
                                    UserAge = Convert.ToInt32(x.Element("UserAge").Value),
                                    Enabled = Convert.ToBoolean(x.Element("Enabled").Value)
                                })
                        .ToList();
            }
            catch
            {
                throw;
            }
            return modelList;
        }

        /// <summary>
        /// 添加会员
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="UserName"></param>
        /// <param name="UserAge"></param>
        /// <param name="UserTell"></param>
        /// <returns></returns>

        public ActionResult SaveUserInfo(int UserId, string UserName, int UserAge, string UserTell)
        {
            try
            {
                XElement userInfoXml = XElement.Load(path);
                XElement root =
                    new XElement("User",
                        new XAttribute("UserId",UserId),
                        new XElement("UserName", UserName),
                        new XElement("UserAge", UserAge),
                        new XElement("Tell", UserTell),
                        new XElement("Enabled", false)
                        );
                userInfoXml.Add(root);
                userInfoXml.Save(path);
            }
            catch (Exception ex)
            {
                return Json(ReturnInfo.Failure("保存失败！"));
            }
            return Json(ReturnInfo.Success());
        }

    }
}